﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SharedResources.Model;
using log4net.Config;


namespace HomesteadTests
{
    [TestFixture]
    public class TestCustomDataTypes
    {
        //[SetUp] called before each test
        //[TearDown] called after each test

        private GeoCoordinate _testGeoCoord;
        private double _mockLatitude;
        private double _mockLongitude;
        private log4net.ILog _log = log4net.LogManager.GetLogger(
  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

       [TestFixtureSetUp]
        public void Init()
       {
           XmlConfigurator.Configure(); 
           var rand = new Random();
           _mockLatitude = rand.NextDouble();
           _mockLongitude = rand.NextDouble();
           _mockObjectId = rand.Next();
           _testSpatioTemporalPoint = new SpatioTemporalPoint(_mockObjectId, DateTime.Now, _mockLatitude, _mockLongitude);
           _testGeoCoord = new GeoCoordinate(_mockLatitude, _mockLongitude);
       }

        [Test]
        public void GeoCoordToString()
        {
            string stringRepresentation = _testGeoCoord.ToString();
            _log.Info("TEST-GEOCOORD-TOSTRING: " + stringRepresentation);
            Assert.AreEqual(stringRepresentation, _mockLatitude.ToString() + "," + _mockLongitude.ToString());
        }

        [Test]
        public void GeoCoordEquality()
        {
            var anotherTestCoord = new GeoCoordinate(_mockLatitude, _mockLongitude);
            _log.Info("TEST-GEOCOORD-ARE-EQUAL: coord1 - " + anotherTestCoord + " coord2 - " + _testGeoCoord);
            Assert.AreEqual(anotherTestCoord, _testGeoCoord);
        }

        private int _mockObjectId;
        private SpatioTemporalPoint _testSpatioTemporalPoint;
        
        [Test]
        public void SpatioTemporalPointGetObjectId()
        {
            _log.Info("TEST_GET_SpatioTemporalPoint_ID: passed in - " + _mockObjectId + " output - " + _testSpatioTemporalPoint.ObjectId);
            Assert.AreEqual(_testSpatioTemporalPoint.ObjectId, _mockObjectId);
        }

        [Test]
        public void SpatioTemporalPointGetTime()
        {
            var testedTimeValue = DateTime.Now;
            var anotherTrajectoryObject = new SpatioTemporalPoint(_mockObjectId, testedTimeValue, _mockLatitude,
                                                               _mockLongitude);
            _log.Info("TEST_GET_SpatioTemporalPoint_TIME_VALUE: passed in - " + testedTimeValue + " output - " + anotherTrajectoryObject.DateTimeRecorded);
            Assert.AreEqual(anotherTrajectoryObject.DateTimeRecorded, testedTimeValue);
        }

        [Test]
        public void SpatioTemporalPointGetPoint()
        {
            _log.Info("TEST_GET_SpatioTemporalPoint_TIME_VALUE: passed in - " + _mockLatitude.ToString() + "," + _mockLongitude.ToString()
                + " output - " + _testSpatioTemporalPoint.SpatialData);
            Assert.AreEqual(_testSpatioTemporalPoint.SpatialData.ToString(), _mockLatitude.ToString() + "," + _mockLongitude.ToString());
        }

        [Test]
        public void SortedGroupedDatasetAdd10UnqiueData()
        {
            var sortedGroupedDataset = new SortedGroupedDataset();
            var rand = new Random();
            for (int i = 0; i < 10; i++)
            {
                var pointToAdd = new SpatioTemporalPoint(rand.Next(), DateTime.UtcNow, rand.NextDouble(),
                                                         rand.NextDouble());
                sortedGroupedDataset.Add(pointToAdd);
                _log.Info("TEST-ADD-TO-SortedGroupedDataset: " + pointToAdd.SpatialData);
            }
            Assert.AreEqual(10, sortedGroupedDataset.GroupedTrajectorySets.Count);
        }

        [Test]
        public void SortedGroupedDatasetAdd10SameData()
        {
            var sortedGroupedDataset = new SortedGroupedDataset();
            var rand = new Random();
            for (int i = 0; i < 10; i++)
            {
                var pointToAdd = new SpatioTemporalPoint(7337, DateTime.UtcNow, rand.NextDouble(),
                                                         rand.NextDouble());
                System.Threading.Thread.Sleep(50); //sleep for 50milliseconds
                sortedGroupedDataset.Add(pointToAdd);
                _log.Info("TEST-ADD-TO-SortedGroupedDataset: " + pointToAdd.SpatialData);
            }
            Assert.AreEqual(1, sortedGroupedDataset.GroupedTrajectorySets.Count);
            SortedList<DateTime, GeoCoordinate> specificSortedValues;
            sortedGroupedDataset.GroupedTrajectorySets.TryGetValue(7337, out specificSortedValues);
            Assert.AreEqual(10, specificSortedValues.Count);
        }


        //[TestFixtureTearDown] 
        //public void Dispose()
        //{
            
        //}

    }
}
